/*
 * 
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */
package utopiaintelpackage.kd;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import main.CommonMethods;

import org.apache.log4j.Logger;

import exceptions.IntelParsingException;

/**
 * This class parses KDs
 * 
 * 
 */
public class KDParser {
   private final String racesregex = CommonMethods.getRacesRegex(true);
   private final String titlesregex = CommonMethods.getTitlesRegex();

   public boolean parseKD(final String input, final KD kd) throws IntelParsingException {
	  int size = kd.getIntel().size();
	  int provsize = kd.getProvs().size();
	  String in = input;
	  in = in.replace("*", "");
	  in = in.replace("(M)", "");
	  in = in.replace("(S)", "");
	  in = in.replace("^", "");
	  in = in.replace("+", "");
	  @SuppressWarnings("serial")
	  Map<String, String> titlesMap = new HashMap<String, String>() {
		 {
			put("1", "Peasant");
			put("2", "Knight");
			put("3", "Lord");
			put("4", "Baron");
			put("5", "Viscount");
			put("6", "Count");
			put("7", "Marquis");
			put("8", "Duke");
			put("9", "Prince");
			put("K", "King");
			put("Q", "Queen");
		 }
	  };
	  try {
		 if (in.matches(".*Current kingdom is .*\\([0-9]{1,2}(:)[0-9]{1,2}\\).*")) {
			String name = in.replaceAll("Current kingdom is ", "");
			name = name.substring(0, name.indexOf('('));
			String loc = in.substring(in.indexOf('('));
			kd.setName(name.trim());
			kd.setLoc(loc.trim());
			kd.setUpdated(new Date(Calendar.getInstance().getTimeInMillis()));
		 } else if (in.matches(".* \\([0-9]{1,2}(:)[0-9]{1,2}\\) Kingdom Analysis")) {
			String name = in.replaceAll(" \\([0-9]{1,2}(:)[0-9]{1,2}\\) Kingdom Analysis", "");
			String loc = in.substring(in.indexOf('('), in.indexOf(')') + 1);
			kd.setName(name.trim());
			kd.setLoc(loc.trim());
			kd.setUpdated(new Date(Calendar.getInstance().getTimeInMillis()));
		 } else if (in.matches(".*This kingdom is currently at war.*")) {
			kd.setAtWar(true);
		 } else if (in.matches("Provinces in Kingdom: [0-9]{1,2}")) {
			kd.setAmountOfProvs(Integer.parseInt(in.replaceAll("[^0-9]", "")));
		 } else if (in.matches("[0-9]{1,2}\\. .* \\[" + racesregex + " - [0-9,]{1,} - [0-9,]{1,}K - [1-9KQ]\\]")) {
			String name = in.replaceFirst("[0-9]{1,2}\\.", "");
			name = name.substring(0, name.indexOf('[')).trim();
			String[] split = in.substring(in.indexOf('[') + 1, in.indexOf(']')).split(" - ");
			String race = CommonMethods.getRacesMapping(split[0]);
			String land = split[1];
			String nw = split[2].replaceAll("[^0-9]", "") + "000";
			String title = titlesMap.get(split[3]);
			kd.addProvince(name, race, "", Integer.parseInt(land.replace(",", "")), Integer.parseInt(nw), title);
		 } else if (in.matches(".*?\t.*?\t" + racesregex + "[ ]{0,1}\t"
			   + "[0-9,]{1,} acres[ ]{0,1}\t[0-9,]{1,}gc[ ]{0,1}\t[0-9,]{1,}gc[ ]{0,1}\t" + titlesregex)) {
			String[] split = in.split("\t");
			String name = split[1].trim();
			String race = split[2].trim();
			String land = split[3].replace(" acres", "").trim();
			String nw = split[4].replace("gc", "").trim();
			String title = split[6].trim();
			kd.addProvince(name, race, "", Integer.parseInt(land.replace(",", "")), Integer.parseInt(nw.replace(",", "")), title);
		 }
	  } catch (NumberFormatException e) {
		 Logger.getLogger("Error").error("", e);
	  }
	  return size < kd.getIntel().size() || provsize < kd.getProvs().size();
   }
}
